<?php
/**
 * Created by PhpStorm.
 * User: zhangfulai
 * Date: 2018/10/3
 * Time: 下午5:30
 */
namespace app\obd\controller;
use think\Config;
use think\Controller;
use think\Db;
use app\obd\model\CarModel;
use think\Request;

class Car extends Controller{
    /*
    * Notes: 汽车信息三级联动
    * Create Date: 2018/10/3
    * Create Time: 下午5:46
    * Create by :张福来
    * param :
    */
    public function selectCarop(Request $request){
        $Car = new CarModel();
        $postData = $request->post();
        if(empty($postData["p_id"])){
            $w["P_ID"] = 0;
        }else{
            $w["P_ID"] = $postData["p_id"];
        }
            $w["CAR_STATUS"] = array('NEQ',9);
        $chek = $this->validate($postData,"CarValidate.selectCarop");
        if($chek !== true){
            return apiResponse("201",$chek,"");
        }
        if(!empty($postData["name"])){
            $w['NAME'] = array('LIKE','%'.trim($postData["name"]).'%');
            $world = $postData["name"].$postData['p_id'];
        }else{
            $world = "-".$postData['p_id'];
        }

        $result = $Car ->selectcar($w,$world);
        foreach ($result as $key => $value) {
            $result[$key]['index'] = $key;
        }
        if(!empty($result)){
            return apiResponse("200","成功",$result);
        }else{
            return apiResponse("201","失败",$result);
        }
    }
    /*
    * Notes: 车辆品牌搜索 大鹏定制
    * Create Date: 2018/10/19
    * Create Time: 下午4:10
    * Create by :张福来
    * param :
    */
    public function selectCaropList(Request $request){
        $Car = new CarModel();
        $postData = $request->post();
        if(empty($postData["pagenum"])){
            $pagenum = 1;
        }else{
            $pagenum = $postData["pagenum"];
        }

        $listNum = 20;
        if(empty($postData["p_id"])){
            $w["P_ID"] = 0;
        }else{
            $w["P_ID"] = $postData["p_id"];
        }
            $w["CAR_STATUS"] = array('NEQ',9);
        $chek = $this->validate($postData,"CarValidate.selectCarop");
        if($chek !== true){
            return apiResponse("201",$chek,"");
        }
        if(!empty($postData["name"])){
            $w['NAME'] = array('LIKE','%'.trim($postData["name"]).'%');
            $world = $pagenum.$listNum.$postData["name"].$postData['p_id'];
        }else{
            $world = "-".$pagenum.$listNum.$postData['p_id'];
        }

        $result = $Car ->selectcarList($w,$world,$pagenum,$listNum);

        foreach ($result["list"] as $key => $value) {
            $result["list"][$key]['index'] = $key;

            $ishasChild = $Car->ishasChild($result["list"][$key]["ID"]);
            if($ishasChild){
                $result["list"][$key]['ishasChild'] = 1;
            }else{
                $result["list"][$key]['ishasChild'] = 0;
            }
        }
        if(!empty($result["list"])){
            return apiResponse("200","成功",$result);
        }else{
            return apiResponse("201","无更多数据",$result);
        }
    }
    /*
    * Notes:  删除车辆品牌
    * Create Date: 2018/10/20
    * Create Time: 上午11:44
    * Create by :张福来
    * param :
    */
    public function deleteCarop(Request $request){
        $Car = new CarModel();
        $postData = $request->post();
        $check = $this->validate($postData,"CarValidate.deleteCarop");
        if($check !== true){
            return apiResponse("200",$check,"");
        }
        $w["ID"] = $postData["id"];
        $result = $Car->deleteCarop($w);
        if($result){
            return apiResponse(200,'删除成功',"");
        }else{
            return apiResponse(201,'删除失败',"");
        }
    }
   
    /**
     * Notes: 根据id修改车辆品牌名|车系名|车型名
     * Create Date: 2018/10/24
     * Create by :lijie
     */
    public function editCar(Request $request){
        $carModel = new CarModel();
        // 验证
        $param  = $request->post();
        $resVali= $this->validate($param,"CarValidate.editCar");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $w['ID']                = $param['ID'];
        $w['CAR_STATUS']        = 0;
        $param['MODIFY_TIME']   = date("Y-m-d H:i:s",time());
        $resCar = $carModel->editCarType($w,$param);
        if($resCar){
            return apiResponse(200,'修改成功',$resCar);
        }else {
            return apiResponse(201,'修改失败','');
        }
    }

    /**
     * Notes: 根据上一级id添加新类别车系|车型|品牌
     * Create Date: 2018/10/24
     * Create by :lijie
     */
    public function addCarName(Request $request){
        $carModel = new CarModel();
        // 验证
        $param  = $request->post();
        $resVali= $this->validate($param,"CarValidate.addCarName");
        if($resVali !== true){
            return apiResponse(201,$resVali,'');
        }
        $uuid = uniqid();
        $html = <<<sql
          INSERT INTO orange_carmodel (ID,P_ID,TYPE,NAME,CREATE_TIME) VALUES 
          ('$uuid','{$param['P_ID']}','{$param['TYPE']}','{$param['NAME']}',now())
sql;
        $num = $carModel->execute($html);
        if($num){
            return apiResponse(200,'添加成功',$uuid);
        }else {
            return apiResponse(201,'添加失败','');
        }
    }
}